codeforces-1017c The Phone Number(构造+思维+数学)

描述

传送门:The Phone Number

Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number!

The only thing Mrs. Smith remembered was that any permutation of n can be a secret phone number. Only those permutations that minimize secret value might be the phone of her husband.

The sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once.

The secret value of a phone number is defined as the sum of the length of the longest increasing subsequence (LIS) and length of the longest decreasing subsequence (LDS).

A subsequence ai1,ai2,…,aik where 1≤i1<i2<…<ik≤n is called increasing if ai1<ai2<ai3<…<aik. If ai1>ai2>ai3>…>aik, a subsequence is called decreasing. An increasing/decreasing subsequence is called longest if it has maximum length among all increasing/decreasing subsequences.

For example, if there is a permutation [6,4,1,7,2,3,5], LIS of this permutation will be [1,2,3,5], so the length of LIS is equal to 4. LDS can be [6,4,1], [6,4,2], or [6,4,3], so the length of LDS is 3.

Note, the lengths of LIS and LDS can be different.

So please help Mrs. Smith to find a permutation that gives a minimum sum of lengths of LIS and LDS.

输入描述

The only line contains one integer n (1≤n≤105) — the length of permutation that you need to build.

输出描述

Print a permutation that gives a minimum sum of lengths of LIS and LDS.

If there are multiple answers, print any.

示例

输入

1
2
3
4

2

输出

1
2
3
3 4 1 2

2 1

Hint

In the first sample, you can build a permutation [3,4,1,2]. LIS is [3,4] (or [1,2]), so the length of LIS is equal to 2. LDS can be ony of [3,1], [4,2], [3,2], or [4,1]. The length of LDS is also equal to 2. The sum is equal to 4. Note that [3,4,1,2] is not the only permutation that is valid.

In the second sample, you can build a permutation [2,1]. LIS is [1] (or [2]), so the length of LIS is equal to 1. LDS is [2,1], so the length of LDS is equal to 2. The sum is equal to 3. Note that permutation [1,2] is also valid.

题解

题目大意

给定一个数n,构造一个1-n的序列使得其中的最长上升子序列和最长下降子序列的和最短

思路

将 n 分成 m 块, 其中每一块中的数字是递增的,各个块之间是递减的,则最长上升子序列的长度和最长下降子序列的长度和最小,为m+(n/m),由基本不等式可知,当 m 为 √n 时 m+(n/m) 的值最小。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <stack>
#include <cmath>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <map>
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3 +5;
using namespace std;

int main(){
int n;
while(cin >> n){
int siz = ceil(sqrt(n));
int tot = 0;
for(int i = 1; i <= siz; i++){
for(int j = 1; j <= siz; j++){
int prt = n-i*siz+j;
if(prt > 0){
cout << prt << " ";
}
tot++;
}
}
for(int i = 1; i <= n-tot; i++){
cout << i << " ";
}
cout << endl;
}
}